//
//  CartTableViewCell.m
//  AppleCilent
//
//  Created by Nail Sharipov on 07/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "CartTableViewCell.h"
#import "CartTableView.h"


@implementation CartTableViewCell
{
    CartOrder* order;
}
+ (float) getHeight
{
    return 48.0f;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect rect = [self frame];
        float height = [CartTableViewCell getHeight];
        float width = rect.size.width - 16.0f; // 8.0f view padding
        float margin = 4.0f;
        float contentHeight = height - 2.0f * margin;
        
        float x = width - contentHeight - margin;
        float y = margin;
        
        UIButton* delButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, contentHeight, contentHeight)];
        UIImage* crossImage = [UIImage imageNamed:@"crossButton.png"];
        [delButton setBackgroundImage:crossImage forState:UIControlStateNormal];
        [delButton addTarget:self action:@selector(delPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:delButton];

        float priceWidth = 60.0f;
        x = x - margin - priceWidth;
        
        self.price = [[UILabel alloc] initWithFrame:CGRectMake(x, y, priceWidth, contentHeight)];
        [self.price setTextColor: [UIColor blackColor]];
        [self.price setBackgroundColor:[UIColor clearColor]];
        [self.price setAdjustsFontSizeToFitWidth:NO];
        [self.price setAdjustsLetterSpacingToFitWidth:NO];
        [self.price setTextAlignment:NSTextAlignmentRight];
        [self.price setFont: [UIFont systemFontOfSize: 14.0f]];
        [self addSubview:self.price];
        
        float quantityWidth = 40.0f;
        x = x - margin - quantityWidth;
        
        self.quantity = [[UILabel alloc] initWithFrame:CGRectMake(x, y, quantityWidth, contentHeight)];
        [self.quantity setTextColor: [UIColor blackColor]];
        [self.quantity setBackgroundColor:[UIColor clearColor]];
        [self.quantity setAdjustsFontSizeToFitWidth:NO];
        [self.quantity setAdjustsLetterSpacingToFitWidth:NO];
        [self.quantity setTextAlignment:NSTextAlignmentCenter];
        [self.quantity setFont: [UIFont systemFontOfSize: 14.0f]];
        [self addSubview:self.quantity];
        
        x = x - margin;
        
        self.name = [[UILabel alloc] initWithFrame:CGRectMake(margin, y, x - margin, contentHeight)];
        [self.name setTextColor: [UIColor blackColor]];
        [self.name setBackgroundColor:[UIColor clearColor]];
        [self.name setAdjustsFontSizeToFitWidth:NO];
        [self.name setAdjustsLetterSpacingToFitWidth:NO];
        [self.name setTextAlignment:NSTextAlignmentLeft];
        [self.name setFont: [UIFont systemFontOfSize: 14.0f]];
        [self addSubview:self.name];
        
        [[self contentView] setBackgroundColor:[UIColor colorWithWhite:0.95f alpha:1.0f]];
        
    }
    return self;
}

- (void)awakeFromNib
{
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

-(void)delPressed:(id)sender
{
    NSLog(@"del pressed: %@", [[order getGoods] getName]);
    
    [(CartTableView *)[[self superview] superview] removeRow:self];
}

- (void) setOrder:(CartOrder*) cartOrder
{
    order = cartOrder;
    GoodsDomain* goods = [order getGoods];
    [self.name setText:[goods getName]];
    int price = (int)[goods getPrice];
    [self.price setText:[NSString stringWithFormat:@"%i р.", price]];
    int quantity = [order getQuantity];
    [self.quantity setText:[NSString stringWithFormat:@"%i", quantity]];
}

- (CartOrder*)getOrder
{
    return order;
}

@end
